# To change this template, choose Tools | Templates
# and open the template in the editor.

module AdministratorSystem
  class AdministratorSystemProxy

    def self.exist_order_for_material_and_customer?(material, customer)
      begin
        if material.nil? || customer.nil?
          return false

        else
          # se traducen los codigos de las entidades
          material_id_old = translate_material_id material
          customer_id_old, customer_type_old = translate_customer_id customer

          # se realiza la consulta
          puts "material_id_old = " + material_id_old.to_s + " - customer_id_old = " + customer_id_old.to_s + " - customer_type_old = " + customer_type_old.to_s
          count = Order.count(:conditions => ["cod_material = ? and tipo_cliente = ? and cod_cliente = ?", material_id_old, customer_type_old, customer_id_old])
          puts "counts: " + count.to_s
          return count > 0
        end
      rescue
        puts "ocurrio una excepcion al buscar las ordenes!"
        return false
      end
    end

    def self.exist_invoice_for_material_and_customer?(material, customer)
      begin
        if material.nil? || customer.nil?
          return false

        else
          # se traducen los codigos de las entidades
          material_id_old = translate_material_id material
          customer_id_old, customer_type_old = translate_customer_id customer

          # se realiza la consulta
          count = InvoiceLine.count(:include => :invoice, :conditions => ["cod_material = ? and tipo_cliente = ? and cod_cliente = ?", material_id_old, customer_type_old, customer_id_old])
          return count > 0
        end
      rescue
        puts "ocurrio una excepcion al buscar las facturas!"
        return false
      end
    end

    def self.exist_customer_catalog_for_material?(material, customer)
      begin
        if material.nil? || customer.nil?
          return false

        else
          # se traducen los codigos de las entidades
          material_id_old = translate_material_id material
          customer_id_old, customer_type_old = translate_customer_id customer

          # se realiza la consulta
          count = CustomerMaterialCatalog.count(:conditions => ["cod_material = ? and tipo_cliente = ? and cod_cliente = ?", material_id_old, customer_type_old, customer_id_old])
          return count > 0
        end
      rescue
        puts "ocurrio una excepcion al buscar en el catalogo del cliente!"
        return false
      end
    end

    def self.material_stock(material)
      begin
        if material.nil?
          return 0

        else
          # se traducen los codigos de las entidades
          material_id_old = translate_material_id material

          # se realiza la consulta
          material_stock = MaterialStock.find(:first, :conditions => ["cod_mat = ?", material_id_old])
          if material_stock.nil?
            return 0
          else
            return material_stock.stock_mat
          end
        end
      rescue
        puts "ocurrio una excepcion al buscar en el stock del cliente!"
        return 0
      end
    end

    private

    def self.translate_material_id material
        # se debe hacer la traduccion del codigo del material del sistema web al sistema administrativo
        #Segun lo acordado, los codigos van a ser igualesde un sistema a otro
        return material.id
    end

    def self.translate_customer_id customer
        # se debe hacer la traduccion del codigo del cliente del sistema web al sistema administrativo
        #TODO:falta traduccir codigo y tipo (customer.id, customer.class.to_s)
        return 1, 1
    end

  end
end
